package com.qinhao.loveCrowdFunding.service;

import com.qinhao.loveCrowdFunding.enums.MessageStateEnum;
import com.qinhao.loveCrowdFunding.mapper.MessageMapper;
import com.qinhao.loveCrowdFunding.pojo.*;
import com.qinhao.loveCrowdFunding.req.MessageStatusConvertByIdRequest;
import com.qinhao.loveCrowdFunding.req.MessageStatusConvertRequest;
import com.qinhao.loveCrowdFunding.req.UserMessageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    MessageMapper messageMapper;

    public UserMessageDTO getUserMessage(UserMessageRequest userMessageRequest){
        UserMessageDTO userMessageDTO = UserMessageDTO.builder().build();
        List<Message> unReadMessage = messageMapper.getUnReadMessageByUserId(userMessageRequest);
        List<Message> readMessage = messageMapper.getReadMessageByUserId(userMessageRequest);
        List<Message> recycleMessage = messageMapper.getRecycleMessageByUserId(userMessageRequest);
        userMessageDTO.setUnreadMessage(unReadMessage);
        userMessageDTO.setReadMessage(readMessage);
        userMessageDTO.setRecycleMessage(recycleMessage);
        return userMessageDTO;
    }

    public MessageStatusConvertDTO unRead2ReadById(MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        messageMapper.unRead2ReadById(messageStatusConvertRequest);
        MessageStatusConvertDTO messageStatusConvertDTO = MessageStatusConvertDTO.builder().build();
        messageStatusConvertDTO.setId(messageStatusConvertRequest.getId());
        return messageStatusConvertDTO;
    }

    public MessageStatusConvertDTO read2RecycleById(MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        messageMapper.read2RecycleById(messageStatusConvertRequest);
        MessageStatusConvertDTO messageStatusConvertDTO = MessageStatusConvertDTO.builder().build();
        messageStatusConvertDTO.setId(messageStatusConvertRequest.getId());
        return messageStatusConvertDTO;
    }

    public MessageStatusConvertDTO recycle2ReadById(MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        messageMapper.recycle2ReadById(messageStatusConvertRequest);
        MessageStatusConvertDTO messageStatusConvertDTO = MessageStatusConvertDTO.builder().build();
        messageStatusConvertDTO.setId(messageStatusConvertRequest.getId());
        return messageStatusConvertDTO;
    }

    public MessageStatusConvertDTO handleAllMessage(MessageStatusConvertRequest messageStatusConvertRequest) {
        MessageStatusConvertDTO messageStatusConvertDTO = MessageStatusConvertDTO.builder().build();
        if (messageStatusConvertRequest.getState().equals(MessageStateEnum.UNREAD.getState())) {
            messageMapper.unRead2Read();
            messageStatusConvertDTO.setId(0);
        }else if (messageStatusConvertRequest.getState().equals(MessageStateEnum.READ.getState())) {
            messageMapper.read2Recycle();
            messageStatusConvertDTO.setId(1);
        }else if (messageStatusConvertRequest.getState().equals(MessageStateEnum.RECYCLE.getState())) {
            messageMapper.deleteRecycle();
            messageStatusConvertDTO.setId(2);
        }
        return messageStatusConvertDTO;
    }

    public MessageWriteByUserIdDTO writeUnreadMessageByUserId(MessageWriteByUserIdEntity messageWriteByUserIdEntity) {
        MessageWriteByUserIdDTO messageWriteByUserIdDTO = MessageWriteByUserIdDTO.builder().build();
        messageMapper.addUnreadMessageByUserId(messageWriteByUserIdEntity);
        if (messageWriteByUserIdEntity.getId() != null) {
            messageWriteByUserIdDTO.setId(messageWriteByUserIdEntity.getId());
        }
        return messageWriteByUserIdDTO;
    }
}
